<?php

/**
 * This is the model class for table "kodo_kodostudios_language".
 */
require_once(dirname(__FILE__) . '/base/LanguageBase.php');
class Language extends LanguageBase
{
    const STATUS_DEACTIVE = 'N', STATUS_ACTIVE = 'Y';

    public $tablePrefix = '';

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
//        if($this->getTablePrefix()!='')
        return $this->getTablePrefix() . 'language';
    }

    /**
     * Get table prefix for each site language to be added.
     * @return string
     */
    public function getTablePrefix()
    {
        $siteId = isset($_REQUEST['siteId']) ? $_REQUEST['siteId'] : 0;
        if ($siteId > 0) {
            $site = Site::model()->findByPk($siteId);
            if ($site)
                return $site->table_prefix;
            else return SITE_TABLE_PREFIX;
        }

        return SITE_TABLE_PREFIX;
    }

    public function getStatusOptions()
    {
        return array(
            self::STATUS_DEACTIVE => 'Deactive',
            self::STATUS_ACTIVE => 'Active',
        );
    }

    public function getStatusText()
    {
        $options = $this->getStatusOptions();
        return isset($options[$this->status]) ? $options[$this->status] : Yii::t('app', 'Unknown {att}', array('{att}' => $this->status));
    }


    public function getLanguageName()
    {
        $languageList = LocaleManager::getStandardLocales();
        return isset($languageList[$this->locale_id]) ? $languageList[$this->locale_id] : Yii::t('app', 'Unknown {att}', array('{att}' => $this->locale_id));
    }

    /**
     * Get all language option, listing all language friendly
     * @static
     * @return array
     */
    public static function getLanguageOption()
    {
        $languageList = LocaleManager::getStandardLocales();
        $models = self::model()->findAll('status=true');
        $option = array();
        if (count($models)) {
            foreach ($models as $model) {
                $option[$model->locale_id] = $languageList[$model->locale_id];
            }
            return $option;
        }
        return array();
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('locale_id', $this->locale_id, true);
        $criteria->compare('status', $this->status);
        $criteria->compare('site_id', $this->site_id);

        return new CActiveDataProvider(get_class($this), array(
            'criteria' => $criteria,
            'sort' => array(
                'defaultOrder' => 'locale_id'),
        ));
    }

}